home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / hplip / timedate.py < prev    next >
Text File  |  2008-10-13  |  6KB  |  217 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22.  
  23. __version__ = '1.0'
  24. __title__ = 'Time/Date Utility'
  25. __doc__ = "Set the time and date on an HP Officejet."
  26.  
  27. # Std Lib
  28. import sys
  29. import re
  30. import getopt
  31. import struct
  32. import operator
  33. import os
  34.  
  35. # Local
  36. from base.g import *
  37. from base.codes import *
  38. from base import device, status, utils, pml
  39. from prnt import cups
  40.  
  41. try:
  42.     from fax import faxdevice
  43. except ImportError:
  44.     log.error("Unable to load fax services for HPLIP (required for hp-timedate). Exiting.")
  45.     sys.exit(1)
  46.     
  47.  
  48. USAGE = [(__doc__, "", "name", True),
  49.          ("Usage: timedate.py [PRINTER|DEVICE-URI] [OPTIONS]", "", "summary", True),
  50.          utils.USAGE_ARGS,
  51.          utils.USAGE_DEVICE,
  52.          utils.USAGE_PRINTER,
  53.          utils.USAGE_SPACE,
  54.          utils.USAGE_OPTIONS,
  55.          utils.USAGE_BUS1, utils.USAGE_BUS2,
  56.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2,
  57.          utils.USAGE_HELP,
  58.          utils.USAGE_SPACE,
  59.          utils.USAGE_NOTES,
  60.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  61.         ]
  62.  
  63. def usage(typ='text'):
  64.     if typ == 'text':
  65.         utils.log_title(__title__, __version__)
  66.  
  67.     utils.format_text(USAGE, typ, __title__, 'timedate.py', __version__)
  68.     sys.exit(0)
  69.  
  70.  
  71. PML_ERROR_CODES = {
  72.     pml.ERROR_OK_END_OF_SUPPORTED_OBJECTS: "OK: End of supported objects",
  73.     pml.ERROR_OK_NEAREST_LEGAL_VALUE_SUBSITUTED: "OK: Nearest legal value substituted",
  74.     pml.ERROR_UNKNOWN_REQUEST: "Unknown request",
  75.     pml.ERROR_BUFFER_OVERFLOW: "Buffer overflow",
  76.     pml.ERROR_COMMAND_EXECUTION: "Command execution",
  77.     pml.ERROR_UNKNOWN_OID: "Unknown OID",
  78.     pml.ERROR_OBJ_DOES_NOT_SUPPORT_SPECIFIED_ACTION: "Object does not support action",
  79.     pml.ERROR_INVALID_OR_UNSUPPORTED_VALUE: "Invalid or unsupported value",
  80.     pml.ERROR_PAST_END_OF_SUPPORTED_OBJS: "Past end of supported objects",
  81.     pml.ERROR_ACTION_CANNOT_BE_PERFORMED_NOW: "Action cannot be performed now",
  82.     pml.ERROR_SYNTAX: "Syntax",
  83. }
  84.  
  85.  
  86. try:
  87.  
  88.     try:
  89.         opts, args = getopt.getopt(sys.argv[1:],
  90.                                     'p:d:hl:b:g',
  91.                                     ['printer=',
  92.                                       'device=',
  93.                                       'help',
  94.                                       'help-rest',
  95.                                       'help-man',
  96.                                       'help-desc',
  97.                                       'logging=',
  98.                                       'bus=',
  99.                                     ]
  100.                                   )
  101.     except getopt.GetoptError, e:
  102.         log.error(e.msg)
  103.         usage()
  104.  
  105.     printer_name = None
  106.     device_uri = None
  107.     bus = device.DEFAULT_PROBE_BUS
  108.     log_level = logger.DEFAULT_LOG_LEVEL
  109.  
  110.     if os.getenv("HPLIP_DEBUG"):
  111.         log.set_level('debug')
  112.  
  113.     for o, a in opts:
  114.         if o in ('-h', '--help'):
  115.  
  116.             usage()
  117.  
  118.         elif o == '--help-rest':
  119.             usage('rest')
  120.  
  121.         elif o == '--help-man':
  122.             usage('man')
  123.  
  124.         elif o == '--help-desc':
  125.             print __doc__,
  126.             sys.exit(0)
  127.  
  128.         elif o in ('-p', '--printer'):
  129.             if a.startswith('*'):
  130.                 printer_name = cups.getDefaultPrinter()
  131.                 log.debug(printer_name)
  132.                 
  133.                 if printer_name is not None:
  134.                     log.info("Using CUPS default printer: %s" % printer_name)
  135.                 else:
  136.                     log.error("CUPS default printer is not set.")
  137.                 
  138.             else:
  139.                 printer_name = a
  140.  
  141.         elif o in ('-d', '--device'):
  142.             device_uri = a
  143.  
  144.         elif o in ('-b', '--bus'):
  145.             bus = [x.lower().strip() for x in a.split(',')]
  146.             if not device.validateBusList(bus):
  147.                 usage()
  148.  
  149.         elif o in ('-l', '--logging'):
  150.             log_level = a.lower().strip()
  151.             if not log.set_level(log_level):
  152.                 usage()
  153.  
  154.         elif o == '-g':
  155.             log.set_level('debug')
  156.  
  157.         #elif o in ('-v', '--value'):
  158.         #    print a
  159.  
  160.     if device_uri and printer_name:
  161.         log.error("You may not specify both a printer (-p) and a device (-d).")
  162.         usage()
  163.  
  164.     utils.log_title(__title__, __version__)
  165.     
  166.     if os.getuid() == 0:
  167.         log.warn("hp-timedate should not be run as root.")
  168.  
  169.     if not device_uri and not printer_name:
  170.         try:
  171.             device_uri = device.getInteractiveDeviceURI(bus, filter={'fax-type' : (operator.gt, 0)})
  172.             if device_uri is None:
  173.                 sys.exit(1)
  174.         except Error:
  175.             log.error("Error occured during interactive mode. Exiting.")
  176.             sys.exit(0)
  177.  
  178.     try:
  179.         d = faxdevice.FaxDevice(device_uri, printer_name, disable_dbus=True)
  180.     except Error, e:
  181.         if e.opt == ERROR_DEVICE_DOES_NOT_SUPPORT_OPERATION:
  182.             log.error("Device does not support setting time/date.")
  183.             sys.exit(1)
  184.         else:
  185.             log.error(e.msg)
  186.             sys.exit(1)
  187.  
  188.     if d.device_uri is None and printer_name:
  189.         log.error("Printer '%s' not found." % printer_name)
  190.         sys.exit(1)
  191.  
  192.     if d.device_uri is None and device_uri:
  193.         log.error("Malformed/invalid device-uri: %s" % device_uri)
  194.         sys.exit(1)
  195.  
  196.     user_cfg.last_used.device_uri = d.device_uri
  197.  
  198.     try:
  199.         try:
  200.             d.open()
  201.         except Error:
  202.             log.error("Unable to open device. Exiting. ")
  203.             sys.exit(1)
  204.  
  205.         try:
  206.             d.setDateAndTime()
  207.         except Error:
  208.             log.error("An error occured!")
  209.     finally:
  210.         d.close()
  211.  
  212. except KeyboardInterrupt:
  213.     log.error("User exit")
  214.  
  215. log.info("")
  216. log.info('Done.')
  217.